/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sys.screen;

import java.sql.SQLException;
import java.util.List;
import sys.bean.EmpresaBean;
import sys.bean.VeiculoBean;
import sys.dao.EmpresaDao;
import sys.dao.VeiculoDao;
import sys.utils.LetterUpper;
import sys.utils.Mensagens;
import sys.utils.Utils;

/**
 *
 * @author Administrador
 */
public class CCadastroVeiculo extends javax.swing.JDialog {

    List<EmpresaBean> listaEmpresas;

    EmpresaDao daoEmpresa = new EmpresaDao();
    VeiculoDao daoVeiculo = new VeiculoDao();
    VeiculoBean veiculo;

    private boolean inserir;
    private CPesquisaVeiculo telaPesquisaVeiculo;

    /**
     * Creates new form CCadastroEmpresa
     *
     * @param parent
     * @param modal
     */
    public CCadastroVeiculo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        Utils.escClose(this);
        atualizaEmpresas();
    }

    public CCadastroVeiculo(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        telaPesquisaVeiculo = (CPesquisaVeiculo) parent;
        atualizaEmpresas();
    }

    public void carregaVeiculo(int id) {
        try {
            veiculo = daoVeiculo.consultaVeiculo(id);
            txCodigo.setText(veiculo.getIdVeiculo() + "");
            txModelo.setText(veiculo.getModelo());
            txPlaca.setText(veiculo.getPlaca());
            txAnoFabricacao.setText(veiculo.getAno() + "");
            txRenavam.setText(veiculo.getRenavam());
            txChassi.setText(veiculo.getChassi());
            txObs.setText(veiculo.getObs());

            cbEmpresa.getModel().setSelectedItem(veiculo.getEmpresa().getIdEmpresa() + "-" + veiculo.getEmpresa().getRazaoSocial());

        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao consultar dados da empresa\n" + ex.getMessage());
        }

    }

    private VeiculoBean retornaObjeto() {
        VeiculoBean v = new VeiculoBean();
        EmpresaBean e = new EmpresaBean();

        if (!isInserir()) {
            v.setIdVeiculo(Integer.parseInt(txCodigo.getText()));
        }

        v.setModelo(txModelo.getText());
        v.setPlaca(txPlaca.getText().toUpperCase());
        v.setAno(Integer.parseInt(txAnoFabricacao.getText()));
        v.setRenavam(txRenavam.getText());
        v.setChassi(txChassi.getText());
        v.setObs(txObs.getText());

        e.setIdEmpresa(listaEmpresas.get(cbEmpresa.getSelectedIndex() - 1).getIdEmpresa());

        v.setEmpresa(e);

        return v;
    }

    private void atualizaEmpresas() {
        try {
            cbEmpresa.removeAllItems();
            listaEmpresas = daoEmpresa.listaEmpresas("");
            cbEmpresa.addItem("<< SELECIONE EMPRESA >>");
            for (EmpresaBean emp : listaEmpresas) {
                cbEmpresa.addItem(emp.getIdEmpresa() + "-" + emp.getRazaoSocial());
            }
        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao atualizar empresas\n" + ex.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txAnoFabricacao = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txRenavam = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txModelo = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txChassi = new javax.swing.JTextField();
        cbEmpresa = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        txPlaca = new javax.swing.JFormattedTextField();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txObs = new javax.swing.JTextArea();
        jPanel7 = new javax.swing.JPanel();
        btnSalvar4 = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("MANUTENÇÃO DE CADASTRO");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DADOS DA EMPRESA:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 10))); // NOI18N
        jPanel1.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel1.setText("CODIGO:");

        txCodigo.setEditable(false);
        txCodigo.setBackground(new java.awt.Color(245, 245, 245));
        txCodigo.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txCodigo.setForeground(new java.awt.Color(51, 51, 255));
        txCodigo.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel2.setText("ANO FAB.");

        txAnoFabricacao.setDocument(new LetterUpper());
        txAnoFabricacao.setBackground(new java.awt.Color(245, 245, 245));
        txAnoFabricacao.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txAnoFabricacao.setForeground(new java.awt.Color(51, 51, 255));
        txAnoFabricacao.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel3.setText("RENAVAM:");

        txRenavam.setDocument(new LetterUpper());
        txRenavam.setBackground(new java.awt.Color(245, 245, 245));
        txRenavam.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txRenavam.setForeground(new java.awt.Color(51, 51, 255));
        txRenavam.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel4.setText("PLACA:");

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel5.setText("MODELO:");

        txModelo.setDocument(new LetterUpper());
        txModelo.setBackground(new java.awt.Color(245, 245, 245));
        txModelo.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txModelo.setForeground(new java.awt.Color(51, 51, 255));
        txModelo.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel6.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel6.setText("CHASSI:");

        txChassi.setDocument(new LetterUpper());
        txChassi.setBackground(new java.awt.Color(245, 245, 245));
        txChassi.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txChassi.setForeground(new java.awt.Color(51, 51, 255));
        txChassi.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txChassi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txChassiActionPerformed(evt);
            }
        });

        cbEmpresa.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        cbEmpresa.setForeground(new java.awt.Color(51, 51, 255));
        cbEmpresa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<< SELECIONE EMPRESA >>" }));

        jLabel11.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel11.setText("EMPRESA:");

        txPlaca.setBackground(new java.awt.Color(245, 245, 245));
        txPlaca.setForeground(new java.awt.Color(51, 51, 255));
        try {
            txPlaca.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("AAA-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txPlaca.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txPlaca.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        jLabel12.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel12.setText("OBSERVAÇÕES:");

        txObs.setDocument(new LetterUpper());
        txObs.setColumns(20);
        txObs.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        txObs.setForeground(new java.awt.Color(51, 51, 255));
        txObs.setLineWrap(true);
        txObs.setRows(5);
        jScrollPane1.setViewportView(txObs);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel11)
                    .addComponent(cbEmpresa, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(26, 26, 26)
                            .addComponent(jLabel5))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txAnoFabricacao, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                                    .addComponent(txCodigo, javax.swing.GroupLayout.Alignment.LEADING))
                                .addComponent(jLabel2))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(txModelo)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel4)
                                        .addComponent(txPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txRenavam, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel3))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel6)
                                        .addComponent(txChassi, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addComponent(jLabel12)
                    .addComponent(jScrollPane1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel4)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(txPlaca)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txAnoFabricacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txRenavam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txChassi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35))
        );

        jPanel7.setBackground(new java.awt.Color(245, 245, 245));
        jPanel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));

        btnSalvar4.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnSalvar4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/salvar.png"))); // NOI18N
        btnSalvar4.setText("SALVAR");
        btnSalvar4.setPreferredSize(new java.awt.Dimension(120, 30));
        btnSalvar4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvar4ActionPerformed(evt);
            }
        });
        jPanel7.add(btnSalvar4);

        btnSair.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/sair.png"))); // NOI18N
        btnSair.setText("SAIR");
        btnSair.setPreferredSize(new java.awt.Dimension(120, 30));
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jPanel7.add(btnSair);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 476, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvar4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvar4ActionPerformed
        try {
            if (isInserir()) {
                daoVeiculo.incluir(retornaObjeto());
                telaPesquisaVeiculo.atualizaVeiculos();
                this.dispose();
            } else {
                daoVeiculo.alterar(retornaObjeto());
                telaPesquisaVeiculo.atualizaVeiculos();
                this.dispose();
            }
        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao cadastrar empresa\n" + ex.getMessage());
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnSalvar4ActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void txChassiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txChassiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txChassiActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSalvar4;
    private javax.swing.JComboBox cbEmpresa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txAnoFabricacao;
    private javax.swing.JTextField txChassi;
    private javax.swing.JTextField txCodigo;
    private javax.swing.JTextField txModelo;
    private javax.swing.JTextArea txObs;
    private javax.swing.JFormattedTextField txPlaca;
    private javax.swing.JTextField txRenavam;
    // End of variables declaration//GEN-END:variables

    public boolean isInserir() {
        return inserir;
    }

    public void setInserir(boolean inserir) {
        this.inserir = inserir;
    }
}
